<template>
    <Form class="exform" ref="formValidate" :model="formValidate" :label-width="80" :rules="ruleValidate">
		<FormItem label="语种名称" prop="names" style="width:50%" >
			<Select v-model="formValidate.names"  style="width:80%" transfer>
			    <Option v-for="item in formItem.languagename" :value="item.dictionary_id" :key="item.dictionary_id"> {{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
			</Select>
		</FormItem>
		<FormItem label="语种等级" prop="languelevel.zh"  style="width:50%">
			<Input v-model="formValidate.languelevel.zh" placeholder="语种等级" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="上传附件">
				<upload ref='upload' v-if="UpImgShow" @imagess='imagess'  :Universalurl='imgUrl'></upload>
		</FormItem>
		<div style="width:90%;">
			<FormItem style="width:100%;">
			<Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
			</FormItem>
		</div>
		<div style="clear:both"></div>
		<Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {

    props: {
        teacher_id: {
            type: [Number,String],
            default: null
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    components: {
        Upload:resolve => {require(['$m/Teacher/TeacherInformation/Upload.vue'], resolve);},
    },
    data () {
      	return {

            infoUrl:`api/teacher/v1/lnbsqualification/find/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式
            spinShow:false,//加载动画
            UpImgShow:false,
            imgUrl:null,

			infoOld:null,
			formItem:[],

        	formValidate: {
				teacher_id:this.teacher_id,
				names :'',    //名称 
				shenhe:1,       //审核          
				languelevel:{
					zh:'',
					mn:'',
					en:'',
				},    //语种等级          
        	},
			ruleValidate: {
				// 名称
				names: [
					{ type:'number',required: true, message: '请选择语言名称', trigger: 'change' }
				],
				//语种等级
				'languelevel.zh': [
					{ required: true, message: '请填写语种等级', trigger: 'blur' }
				],
			}, 
      	}
    },
    methods: {
	 
	 
        /************************************************************
         * 
         * 图片上传回调函数
         * 
         ************************************************************/
        imagess(val){   
          this.formValidate.file_cache_ids=val
		},
		
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用  
            this.$http.get(this.infoUrl).then(val=>{
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formValidate.names = parseInt(this.infoOld.names)//用于默认选的 语言名称 
                this.formValidate.shenhe = parseInt(this.infoOld.shenhe)//用于默认选的 审核
                 
                this.formValidate.languelevel = this.infoOld.languelevel//等级
                this.formValidate.school_id=this.infoOld.school_id;//学校
				this.formValidate.teacher_id=this.infoOld.teacher_id;//教师 
				
                this.imgUrl = this.infoOld.imgs.map(item => {
                    let arr = {               
                        security_check_picture_id:item.id,                       
                        photos_url:item.url 
                    }
                    return arr
                })
                this.UpImgShow = true
                this.spinShow=false//输入框禁用  
            })
			.catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
                this.spinShow=false//输入框禁用  
			});
		},
		
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
		handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {       
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
		},
    },
    created() {
        /************************************************************
         * 
         * 查询资格类型
         * 
         ************************************************************/
		this.$http.get('api/dictionary/v1/dictionary/pidquerys?languagename')
		.then(val=>{
			this.formItem = val.data
		})
		.catch(err => {
			if(err.code && err.code==422) {
				this.$Message.error(err.message);
			}
			this.spinShow=false//输入框禁用
		});
        if(this.infoId==false){
            // add
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/lnbsqualification/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/lnbsqualification/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
  }
</script>
<style lang="less">
@import'./../public.less';
</style>
